package ru.dc.gui.swing;

import ru.dc.object.ServerInfo;
import ru.dc.object.events.hub.SearchResultRecieved;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: deady
 * Date: 31.10.2010
 * Time: 22:01:57
 */
public class SearchResultsTableModel extends AbstractTableModel {

    private String[] columnNames = {"Файл", "Размер", "Пользователь", "Хаб"};
    private Vector<SearchResultRecieved> results = new Vector<SearchResultRecieved>();

    public int getRowCount() {
        return results.size();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0 : return SearchResultRecieved.class;
            case 1 : return Long.class;
            case 3 : return ServerInfo.class;
            default: return String.class;
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0 : return results.get(rowIndex);
            case 1 : return results.get(rowIndex).getSize();
            case 2 : return results.get(rowIndex).getSourceNick();
            case 3 : return results.get(rowIndex).getServerInfo();
        }
        return null;
    }

    public void clear() {
        int size = getRowCount();
        results.clear();
        if (size>0) {
            fireTableRowsDeleted(0, size-1);
        }
    }

    public void addResult(SearchResultRecieved res) {
        results.add(res);
        fireTableRowsInserted(getRowCount()-1, getRowCount()-1);
    }

    public void addResults(SearchResultRecieved... res) {
        int size = getRowCount();
        results.addAll(Arrays.asList(res));
        fireTableRowsInserted(size-1, getRowCount()-1);
    }

    public SearchResultRecieved getResult(int index) {
        return results.get(index);
    }
}
